package com.itheima.controller;

import com.itheima.pojo.Student;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(){
        System.out.println("hello");
        return "hello world";
    }

    //接收前端的数据

    @GetMapping("/simpleParam")
    public String simpleParam(String name){
        System.out.println(name);
        return name;
    }

    @RequestMapping("/simpleParam1")
    //public String simpleParam1(String name,Integer age, String gender){
    public String simpleParam1(Student student){
        System.out.println(student);
        return student.toString();
    }

    @RequestMapping("/complexParam")
    public String complexParam(Student student){
        System.out.println(student);
        return student.toString();
    }

    @RequestMapping("/arrayParam")
    public String arrayParam(String[] hobby){
        System.out.println(Arrays.toString(hobby));
        return Arrays.toString(hobby);
    }

    @RequestMapping("/arrayParam1")
    public String arrayParam1(@RequestParam List<String> hobby){
        System.out.println(hobby);
        return hobby.toString();
    }

    @RequestMapping("/timeParam")
    public String timeParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time){
        System.out.println(time);
        return time.toString();
    }

    //json格式的参数非常重要的  后期前后端分离开发 大分部的数据格式都是 json格式。
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody Student student){
        System.out.println(student);
        return student.toString();
    }

    //路径参数    非常重要
    /*
    传统开发
        http://localhost:8080/getById?id=1
        http://localhost:8080/deleteById?id=1

    restful风格开发
        前端的发送
            get方式  http://localhost:8080/dept/1 查询1号部门
            post方式 http://localhost:8080/dept/1 添加1号部门
            put方式  http://localhost:8080/dept/1 更新1号部门
            delete方式 http://localhost:8080/dept/1 删除1号部门
        后端接收的时候
          @DeleteMapping
          @GetMapping
          @PostMapping
          @PutMapping
               @PathVariable
     */
    @DeleteMapping("/pathParam/{id}")
    public String pathParam(@PathVariable Integer id){
        System.out.println(id);
        return id+"";
    }
}
